//
//  MixingStrategy.m
//  CocoaSynth
//
//  Created by Pierre DUCHENE on 04/03/10.
//  Copyright 2010 Pierre DUCHENE. All rights reserved.
//

#import "MixingStrategy.h"
#import "SampleProtocol.h"
#import "Sample.h"
#import "System.h"

@implementation MixingStrategy

#pragma mark -
#pragma mark Accessors

@synthesize module;

#pragma mark -
#pragma mark Memory Management

- (id)init
{
	if (self = [super init]) {
		module = nil;
	}
	
	return self;
}

- (void)dealloc
{
	[module release];
	module = nil;
	
	[super dealloc];
}

#pragma mark -
#pragma mark ComputingStrategyProtocol

- (id<SampleProtocol>)execute
{
	id<SampleProtocol> result = [[Sample alloc] init];
	
	// Get the system
	System* system = [System sharedSystem];
	
	// Get the input values
	id<SampleProtocol> inputSample = [module inputSample];
	id<SampleProtocol> inputSample2 = [module inputSample2];
	NSArray* inputValues = [inputSample voltages];
	NSArray* inputValues2 = [inputSample2 voltages];
	
	// Create output array
	NSMutableArray* outputValues = [[NSMutableArray alloc] init];
	
	for (int i = 0; i < [system sampleSize]; i++) {
		float a = [[inputValues objectAtIndex:i] floatValue];
		float b = [[inputValues2 objectAtIndex:i] floatValue];
		
		[outputValues insertObject:[NSNumber numberWithFloat:(a+b)/2] atIndex:i];
	}
	
	[result setVoltages:[NSArray arrayWithArray:outputValues]];
	[outputValues release];
	
	return [result autorelease];
}

@end
